package com.finance.system.support.utils;

import com.finance.system.support.bean.VerifyResult;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by TENGFEI on 2018/1/4.
 */
public class VerifyUtil {
    public static String VERIFY_SUCCESS = "验证成功";
    public static String VERIFY_ERROR = "验证失败";
    public static String VERIFY_APPKEY_IS_NULL_MSG = "AppKey为空";
    public static String VERIFY_APPKEY_IS_INVALID_MSG = "AppKey无效";
    public static String VERIFY_PHONE_IS_NULL_MSG = "国家代码或手机号码为空";
    public static String VERIFY_INCOMPLETE_NUMBER_MSG = "手机号码格式错误";
    public static String VERIFY_CODE_IS_NULL_MSG = "请求校验的验证码为空";
    public static String VERIFY_CODE_IS_FREQUENTLY_MSG = "请求校验验证码频繁";
    public static String VERIFY_CODE_IS_MISTAKE_MSG = "验证码错误";
    public static String VERIFY_SERVER_IS_CLOSED_MSG = "没有打开服务端验证开关";


    public static VerifyResult verifyCode(String phoneNum,String code) {
        String address = "https://webapi.sms.mob.com/sms/verify";
        MobClient client = null;
        try {
            client = new MobClient (address);
            client.addParam ("appkey", "2386eadb05d83").addParam ("phone", phoneNum)
                .addParam ("zone", "86").addParam ("code", code);
            client.addRequestProperty ("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
            client.addRequestProperty ("Accept", "application/json");
            String result = client.post ();
            VerifyResult verifyResult = JSONUtils.json2Object(result, VerifyResult.class);
            return verifyResult;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace ();
        } catch (KeyManagementException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        } catch (Exception e) {
            e.printStackTrace ();
        } finally {
            client.release ();
        }
        return null;
    }

    public static VerifyResult checkResult(VerifyResult result){

          switch (result.getStatus()) {
                case 200:
                    result.setMessage(VERIFY_SUCCESS);
                   break;
                   case 405:
                       result.setMessage(VERIFY_APPKEY_IS_NULL_MSG);
                   break;
                   case 406:
                       result.setMessage(VERIFY_APPKEY_IS_INVALID_MSG);
                   break;
                   case 456:
                       result.setMessage(VERIFY_PHONE_IS_NULL_MSG);
                   break;
                   case 457:
                       result.setMessage(VERIFY_INCOMPLETE_NUMBER_MSG);
                   break;
                   case 466:
                       result.setMessage(VERIFY_CODE_IS_NULL_MSG);
                   break;
                   case 467:
                       result.setMessage(VERIFY_CODE_IS_FREQUENTLY_MSG);
                   break;
                   case 468:
                       result.setMessage(VERIFY_CODE_IS_MISTAKE_MSG);
                   break;
                   case 474:
                       result.setMessage(VERIFY_SERVER_IS_CLOSED_MSG);
                   break;
             }
        return result;
    }
}
